package com.zyh.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;


@MultipartConfig
@WebServlet("/form-data")
public class RequestFormDataServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getRemoteHost());

        req.setCharacterEncoding("utf-8");
        // 获取文件的操作
        Part part = req.getPart("file");
        InputStream is = part.getInputStream();
        OutputStream os = new FileOutputStream("D:\\IDEA\\idea\\day"+part.getSubmittedFileName());
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        os.close();

//        req.setCharacterEncoding("utf-8");
//        BufferedReader br = req.getReader();
//        StringBuilder sb = new StringBuilder();
//        String line = null;
//        while ((line = br.readLine()) != null) {
//            sb.append(line);
//        }
//        System.out.println(sb);
    }
}
